package com.inei.cenacom.view;

import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.inei.cenacom.R;
import com.inei.cenacom.controller.CaratulaController;
import com.inei.cenacom.model.entity.Caratula;
import com.inei.cenacom.tools.Mensaje;
import com.inei.cenacom.tools.OnSiguienteListener;
import com.inei.cenacom.tools.Validacion;

public class Caratula_2_Fragment extends Fragment {

	private EditText etC2_1, etC2_2, etC2_3, etC2_8, etC2_9;
	private LinearLayout llC2_4_1, llC2_5, llC2_5_1, llC2_6, llC2_7, llC2_8,
			llC2_9;
	private Spinner spC2_4, spC2_5, spC2_6, spC2_7, spC2_8, spC2_9;
	private CaratulaController cCon;
	private Caratula c;
	Validacion v;
	Mensaje m;
	private Button bSiguiente2;
	private OnSiguienteListener onSiguienteListener;

	public OnSiguienteListener getOnSiguienteListener() {
		return onSiguienteListener;
	}

	public void setOnSiguienteListener(OnSiguienteListener onSiguienteListener) {
		this.onSiguienteListener = onSiguienteListener;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_caratula_2, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		iniciarComponentes(view);
		agregarEventos();

		cargarDatos();
	}

	private void cargarDatos() {
		// TODO Auto-generated method stub
		Bundle b = getArguments();
		c = new Caratula();
		c.setID_N(b.getString("ID_N"));
		c.setANIO(b.getInt("ANIO"));
		c = cCon.buscar(c);
		if (c != null) {
			c.setUSR_EDICION(b.getString("USUARIO"));
			etC2_1.setText(c.getII1());
			etC2_2.setText(c.getII2());
			etC2_3.setText(c.getII3());

			spC2_4.setSelection(c.getII4());
			spC2_5.setSelection(c.getII5());
			spC2_6.setSelection(Seleccion(c.getII6()));
			spC2_7.setSelection(c.getII7());
			spC2_8.setSelection(c.getII8());
			spC2_9.setSelection(c.getII9());

			etC2_8.setText(c.getII8_O());
			etC2_9.setText(c.getII9_O());

		}
	}

	public Caratula guardar() throws Exception {

		if (!v.validarTextoA(etC2_1, true)) {
			throw new Exception();
		} else if (!v.validarTextoA(etC2_2, true)) {
			throw new Exception();
		} else if (!v.validarTextoAN(etC2_3, true)) {
			throw new Exception();
		} else if (!v.validarSpinner(spC2_4, true, 0, null)) {
			throw new Exception();
		} else if (spC2_4.getSelectedItemPosition() == 1
				&& spC2_5.getSelectedItemPosition() == 0) {
			m.Error("Debe seleccionar la clase de comisaria");
			spC2_5.setPressed(true);
			throw new Exception();
		} else if (spC2_4.getSelectedItemPosition() == 2
				&& spC2_9.getSelectedItemPosition() == 0) {
			m.Error("Debe seleccionar el tipo de unidad especializada que es la comisaria");
			spC2_9.setPressed(true);
			throw new Exception();
		} else if (spC2_4.getSelectedItemPosition() == 1
				&& spC2_5.getSelectedItemPosition() == 1
				&& (spC2_6.getSelectedItemPosition() == 0 || spC2_7
						.getSelectedItemPosition() == 0)) {
			if (spC2_6.getSelectedItemPosition() == 0) {
				m.Error("Debe seleccionar el tipo de la comisaria");
				spC2_6.setPressed(true);
			} else if (spC2_7.getSelectedItemPosition() == 0) {
				m.Error("Debe seleccionar la categoria de la comisaria");
				spC2_7.setPressed(true);
			}
			throw new Exception();
		} else if (spC2_4.getSelectedItemPosition() == 1
				&& spC2_5.getSelectedItemPosition() == 2
				&& spC2_8.getSelectedItemPosition() == 0) {
			m.Error("Debe seleccionar la especialidad de la comisaria");
			spC2_8.setPressed(true);
			throw new Exception();
		} else if (!v.validarSpinner(spC2_8, false, 5, etC2_8)) {
			throw new Exception();
		} else if (!v.validarSpinner(spC2_9, false, 3, etC2_9)) {
			throw new Exception();
		} else {

			c.setII1(etC2_1.getText().toString());
			c.setII2(etC2_2.getText().toString());
			c.setII3(etC2_3.getText().toString());

			c.setII4(spC2_4.getSelectedItemPosition());
			switch (spC2_4.getSelectedItemPosition()) {
			case 1:
				c.setII5(spC2_5.getSelectedItemPosition());
				c.setII6(spC2_6.getSelectedItem().toString().length() != 1 ? ""
						: spC2_6.getSelectedItem().toString());
				c.setII7(spC2_7.getSelectedItemPosition());
				c.setII8(spC2_8.getSelectedItemPosition());
				c.setII8_O(etC2_8.getText().toString());
				if (spC2_5.getSelectedItemPosition() == 1) {
					c.setII8(0);
					c.setII8_O("");
				} else if (spC2_5.getSelectedItemPosition() == 2) {
					c.setII6("");
					c.setII7(0);
					if (spC2_8.getSelectedItemPosition() == 5) {
						c.setII8_O(etC2_8.getText().toString());
					} else {
						c.setII8_O("");
					}
				}
				c.setII9(0);
				c.setII9_O("");
				break;

			case 2:
				c.setII9(spC2_9.getSelectedItemPosition());
				if (spC2_9.getSelectedItemPosition() == 3) {
					c.setII9_O(etC2_9.getText().toString());
				} else {
					c.setII9_O("");
				}
				c.setII9_O(etC2_9.getText().toString());
				c.setII5(0);
				c.setII6("");
				c.setII7(0);
				c.setII8(0);
				c.setII8_O("");
				break;
			default:
				c.setII5(0);
				c.setII6("");
				c.setII7(0);
				c.setII8(0);
				c.setII9(0);
				c.setII8_O("");
				c.setII9_O("");
				break;
			}

			return c;
		}
	}

	public int Seleccion(String s) {
		if (s != null) {
			String[] v6 = getActivity().getResources().getStringArray(
					R.array.arrayC2_6);
			for (int i = 1; i < v6.length; i++) {
				if (s.equals(v6[i])) {
					return i;
				}
			}
		}
		return 0;
	}

	void cambiar(LinearLayout ll, Spinner sp, boolean estado) {
		if (estado) {
			ll.setVisibility(View.VISIBLE);
		} else {
			sp.setSelection(0);
			ll.setVisibility(View.GONE);
		}
	}

	private void iniciarComponentes(View view) {
		cCon = new CaratulaController(getActivity());
		m = new Mensaje(getActivity());
		v = new Validacion(m);
		
		
		TextView tvCAPII = (TextView)getView().findViewById(R.id.tvCAPII);
		int anio = getArguments().getInt("ANIO");
		if(anio==2011){
			tvCAPII.setBackgroundColor(getActivity().getResources().getColor(R.color.Azul));
			tvCAPII.setTextColor(Color.WHITE);
		}
		bSiguiente2 = (Button) getView().findViewById(R.id.bSiguiente2);
		bSiguiente2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(onSiguienteListener!=null){
					onSiguienteListener.OnSiguiente(2);
				}
			}
		});
		etC2_1 = (EditText) view.findViewById(R.id.etC2_1);
		etC2_2 = (EditText) view.findViewById(R.id.etC2_2);
		etC2_3 = (EditText) view.findViewById(R.id.etC2_3);

		spC2_4 = (Spinner) view.findViewById(R.id.spC2_4);
		llC2_4_1 = (LinearLayout) view.findViewById(R.id.llC2_4_1);

		llC2_5 = (LinearLayout) view.findViewById(R.id.llC2_5);
		spC2_5 = (Spinner) view.findViewById(R.id.spC2_5);

		llC2_5_1 = (LinearLayout) view.findViewById(R.id.llC2_5_1);
		llC2_6 = (LinearLayout) view.findViewById(R.id.llC2_6);
		spC2_6 = (Spinner) view.findViewById(R.id.spC2_6);

		llC2_7 = (LinearLayout) view.findViewById(R.id.llC2_7);
		spC2_7 = (Spinner) view.findViewById(R.id.spC2_7);

		llC2_8 = (LinearLayout) view.findViewById(R.id.llC2_8);
		spC2_8 = (Spinner) view.findViewById(R.id.spC2_8);
		etC2_8 = (EditText) view.findViewById(R.id.etC2_8);

		llC2_9 = (LinearLayout) view.findViewById(R.id.llC2_9);
		spC2_9 = (Spinner) view.findViewById(R.id.spC2_9);
		etC2_9 = (EditText) view.findViewById(R.id.etC2_9);
	}

	private void agregarEventos() {
		spC2_4.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View view, int pos,
					long arg3) {
				switch (pos) {
				case 1:
					spC2_9.setSelection(0);
					llC2_4_1.setVisibility(View.VISIBLE);
					llC2_9.setVisibility(View.GONE);
					break;
				case 2:
					spC2_5.setSelection(0);
					llC2_4_1.setVisibility(View.GONE);
					llC2_9.setVisibility(View.VISIBLE);
					break;
				case 0:
					spC2_5.setSelection(0);
					spC2_9.setSelection(0);
					llC2_4_1.setVisibility(View.GONE);
					llC2_9.setVisibility(View.GONE);
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}

		});

		spC2_5.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				switch (pos) {
				case 1:
					spC2_8.setSelection(0);
					llC2_5_1.setVisibility(View.VISIBLE);
					llC2_8.setVisibility(View.GONE);
					break;
				case 2:
					spC2_6.setSelection(0);
					spC2_7.setSelection(0);
					llC2_5_1.setVisibility(View.GONE);
					llC2_8.setVisibility(View.VISIBLE);
					break;
				case 0:
					spC2_6.setSelection(0);
					spC2_7.setSelection(0);
					spC2_8.setSelection(0);
					llC2_5_1.setVisibility(View.GONE);
					llC2_8.setVisibility(View.GONE);
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});

		spC2_8.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				switch (pos) {
				case 5:
					etC2_8.setVisibility(View.VISIBLE);
					break;
				default:
					etC2_8.setText("");
					etC2_8.setVisibility(View.INVISIBLE);
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});

		spC2_9.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				switch (pos) {
				case 3:
					etC2_9.setVisibility(View.VISIBLE);
					break;
				default:
					etC2_9.setText("");
					etC2_9.setVisibility(View.INVISIBLE);
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});

	}
}
